package sftp.autoconfig;

import com.jcraft.jsch.JSch;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sftp.config.ClientProperties;
import sftp.config.PoolProperties;
import sftp.core.JschLogger;
import sftp.core.SftpPool;
import sftp.core.SftpTemplate;

/**
 * @author hligaty
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({ClientProperties.class, PoolProperties.class})
public class SftpAutoConfiguration {

    @Bean
    public SftpPool sftpPool(ClientProperties clientProperties, PoolProperties poolProperties) {
        JSch.setLogger(new JschLogger(clientProperties.isEnabledLog()));
        return clientProperties.getHosts() == null ?
                new SftpPool(clientProperties, poolProperties) :
                new SftpPool(HostHolder.initHostKeys(clientProperties.getHosts()), poolProperties);
    }

    @Bean
    public SftpTemplate sftpTemplate(SftpPool sftpPool) {
        return new SftpTemplate(sftpPool);
    }
}
